package hrms.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import hrms.entity.Dept;
import hrms.service.DeptService;
import hrms.util.LogUtil;
import hrms.util.Result;

@Controller
@RequestMapping("/dept")
public class DeptController {
	
	@Resource(name="deptService")
	private DeptService deptService;
	
	@ExceptionHandler
	public String exHandle(Exception ex, HttpServletRequest req) {
		LogUtil.saveEx(ex);
		return "error";
	}
	
	@ResponseBody
	@RequestMapping("/loadDept.sls")
	public Result<List<Dept>> executeA(HttpServletRequest req ,HttpServletResponse res){
		String page = req.getParameter("page");
		Result<List<Dept>> result = deptService.findByPage(page,res);
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/deptPage.sls")
	public Result<Map<String,Integer>> executeB2(HttpServletRequest req){
		Result<Map<String,Integer>> result = deptService.pageTotal(req);
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/deleteDept.sls")
	public Result<Object> executeI(HttpServletRequest req){
		String deptId = req.getParameter("id");
		return deptService.deleteDept(deptId, req);
	}
	
	@ResponseBody
	@RequestMapping("/addDept.sls")
	public Result<Object> executeAdd(HttpServletRequest req){
		String deptName = req.getParameter("deptName");
		String deptTask = req.getParameter("deptTask");
		return deptService.addDept(deptName, deptTask);
	}
}